package com.example.config;

import java.util.Arrays;

import javax.sql.DataSource;

import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.DynamicBpmnService;
import org.flowable.engine.FormService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.annotation.Resource;

@Configuration
public class FlowableConfig {
	
	@Resource
	DataSource dataSource;
	
	@Value("${spring.datasource.url}")
	private String url;

	@Value("${spring.datasource.username}")
	private String username;

	@Value("${spring.datasource.password}")
	private String password;

	@Value("${spring.datasource.driver-class-name:com.mysql.jdbc.Driver}")
	private String driver;

	@Resource
	FlowableEventListener flowableEventListener;

	@Bean
	public ProcessEngine getProcessEngine() {
//		ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration().setJdbcUrl(url)
//				.setJdbcUsername(username).setJdbcPassword(password).setJdbcDriver(driver)
//				.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
//
//				// 解决流程图乱码
//				.setActivityFontName("宋体").setLabelFontName("宋体").setAnnotationFontName("宋体");
		
		ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
				.setDataSource(dataSource)
				.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

		cfg.setEventListeners(Arrays.asList(flowableEventListener));

		return cfg.buildProcessEngine();
	}

	// 八大接口
	// 业务流程的定义相关服务
	@Bean
	public RepositoryService repositoryService(ProcessEngine processEngine) {
		return processEngine.getRepositoryService();
	}

	// 流程对象实例相关服务
	@Bean
	public RuntimeService runtimeService(ProcessEngine processEngine) {
		return processEngine.getRuntimeService();
	}

	// 流程任务节点相关服务
	@Bean
	public TaskService taskService(ProcessEngine processEngine) {
		return processEngine.getTaskService();
	}

	// 流程历史信息相关服务
	@Bean
	public HistoryService historyService(ProcessEngine processEngine) {
		return processEngine.getHistoryService();
	}

	// 表单引擎相关服务
	@Bean
	public FormService formService(ProcessEngine processEngine) {
		return processEngine.getFormService();
	}

	// 用户以及组管理相关服务
	@Bean
	public IdentityService identityService(ProcessEngine processEngine) {
		return processEngine.getIdentityService();
	}

	// 管理和维护相关服务
	@Bean
	public ManagementService managementService(ProcessEngine processEngine) {
		return processEngine.getManagementService();
	}

	// 动态流程服务
	@Bean
	public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine) {
		return processEngine.getDynamicBpmnService();
	}
	// 八大接口 end

}